package com.eobard.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * @Author: Eobard Gu
 * @Date: 2025-09-05
 * @Description: 异常处理工具类
 */
public class ExceptionUtils {

    public static void checkNonNull(String args, String message) {
        if (StrUtil.isBlank(args)) throw new IllegalArgumentException(message);
    }

    public static void checkNonNull(Collection<?> args, String message) {
        checkCollection(args, message);
    }

    public static void checkNonNull(Map<?, ?> args, String message) {
        checkCollection(args, message);
    }

    private static void checkCollection(Object args, String message) {
        boolean empty = false;
        if (args instanceof Collection) {
            empty = CollectionUtil.isEmpty((Collection<?>) args);
        } else if (args instanceof Map) {
            empty = CollectionUtil.isEmpty((Map<?, ?>) args);
        }
        if (empty) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void checkNonNull(List<String> args, List<String> messages) {
        if (CollectionUtil.isEmpty(args) || CollectionUtil.isEmpty(messages) || args.size() != messages.size()) {
            throw new IllegalArgumentException("args and messages must not be null and must have same size");
        }
        IntStream.range(0, args.size())
                .filter(i -> StrUtil.isBlank(args.get(i)))
                .findFirst()
                .ifPresent(i -> {
                    throw new IllegalArgumentException(messages.get(i));
                });
    }
}
